package socket;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client4 {
    private Socket socket;

    public Client4(){

        try {
            System.out.println("正在启动客户端....");
            socket = new Socket("localhost",8092);
            System.out.println("客户端启动完毕!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void start(){
        /*
            给服务器写东西
         */
        try {
            PrintWriter pw = new PrintWriter(
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    socket.getOutputStream(),"UTF-8"
                            )
                    )
            ,true);
            Scanner scan = new Scanner(System.in);
            System.out.println("输入吧: ");
            while (true){
                String line = scan.nextLine();
                if ("exit".equals(line)){
                    break;
                }
                pw.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        Client4 client4 = new Client4();
        client4.start();
    }
}
